// pages/smartlife/post_auto_insurance/insurance_detail/insurance_detail.js
var util = require('../../../../utils/util.js');
var common = require("../../../../utils/common.js");
//获取应用实例
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    plate: '',
    phone: '',
    companyArray: ['人保', '太平洋', '平安', '天安', '阳光', '人寿', '太平', '紫金', '大地'],
    companyIndex:0,
    third: '',
    insurancetype: '',
    remark:"备注信息",
    replyDetail:'',
    finishText: "确认投保",
    deleteText: "删除",
    id: null,
    baseInsurance:"",
    vehicleAndVesselTax:"",
    compulsoryInsurance:"",
    insuranceTotal:"",
    status:2,
    address:'',
    latitude:'',
    longitude:'',
    formId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      id: options.id,
      status: options.status
    })
    if (options.status != 2) {//
      that.setData({
        finishText: "完成"
      })
    }
    if (app.globalData.wangcaiToken) {
      that.detailData()
    } else {
      // console.log("间接获取")
      //调用应用实例的方法获取全局数据
      app.getToken(function (wangcaiToken) {
        //更新数据
        that.detailData()
      })
    }
  },
  addressInput: function (e) {
    this.setData({
      address: e.detail.value
    })
  },
  location: function (e) {
    var that = this
    wx.chooseAddress({
      success: function (res) {
        that.setData({
          address: res.provinceName + res.cityName + res.countyName + res.detailInfo
        })
        // console.log(res.userName)
        // console.log(res.postalCode)
        // console.log(res.provinceName)
        // console.log(res.cityName)
        // console.log(res.countyName)
        // console.log(res.detailInfo)
        // console.log(res.nationalCode)
        // console.log(res.telNumber)
      }
    })
    // wx.chooseLocation({
    //   success: function (res) {
    //     that.setData({
    //       address: res.address,
    //       latitude: res.latitude,
    //       longitude: res.longitude
    //     })
    //   },
    // })
  },
  btnComplete: function (e) {
    var that = this
    if (that.data.status!=2) {
      wx.navigateBack({
        delta: 1
      })
      return
    }
    // 设置按钮加载状态
    this.setData({
      btnFinishLoading: true,
      finishText: "申请中…",
      formId: e.detail.formId
    })
    that.submitData(function (res) {
      //更新按钮状态
      that.setData({
        btnFinishLoading: false,
        finishText: "确认报价"
      })
      if (res.status == -1) {
      } else {
        wx.showModal({
          title: '报价成功',
          content: '我们很快会由工作人员与您电话联系投保细节，请保持电话畅通',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              //刷新上一页数据
              var pages = getCurrentPages();
              if (pages.length > 1) {
                //上一个页面实例对象
                var prePage = pages[pages.length - 2];
                prePage.getData()
              }
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      }
    })
  },
  btnDelete: function (e) {
    var that = this
    that.deleteRecord(function (res) {
      if (res.status == -1) {
      } else {
        //刷新上一页数据
        var pages = getCurrentPages();
        if (pages.length > 1) {
          //上一个页面实例对象
          var prePage = pages[pages.length - 2];
          prePage.getData()
        }
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
  submitData: function (cb) {
    var that = this;
    // if(that.data.id == null){
    //新增请求
    wx.request({
      url: common.postUrl() + 'applyAutoInsurance',
      data: {
          id: that.data.id,
          token: app.globalData.wangcaiToken,
          state: 3,
          address: that.data.address,
          latitude: that.data.latitude,
          longitude: that.data.longitude,
          formid: that.data.formId
        },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          typeof cb == "function" && cb(res.data)
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                //刷新上一页数据
                var pages = getCurrentPages();
                if (pages.length > 1) {
                  //上一个页面实例对象
                  var prePage = pages[pages.length - 2];
                  prePage.getData()
                }
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
          returnRes = {
            "status": -1,
            "msg": res.data.msg
          }
          typeof cb == "function" && cb(returnRes)
          return
        }
      }
    })
  },
  deleteRecord: function (cb) {
    var that = this
    wx.showActionSheet({
      itemList: ['删除'],
      itemColor: '#E64340',
      success: function (resSheet) {
        wx.request({
          url: common.postUrl() + 'deleteAutoInsurance',
          data: {
            token: app.globalData.wangcaiToken,
            id: that.data.id
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            if (res.data.status == 1) {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                success: function (e) {
                  // 刷新数据
                  typeof cb == "function" && cb(res.data)
                }
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'success'
              })
            }
          }
        })
      }
    })
  },
  detailData: function (e) {
    //请求该id的详细数据
    var that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    wx.request({
      url: common.postUrl() + 'autoInsuranceDetail',
      data: {
        token: app.globalData.wangcaiToken,
        id: that.data.id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          that.setData({
            plate: res.data.data.plate,
            phone: res.data.data.phone,
            companyIndex: res.data.data.insurancecompany,
            third: res.data.data.thirdpartyinsurance + '万',
            insurancetype: res.data.data.insurancetype.replace('0', '车损险').replace('1', '划痕险').replace('2', '盗抢险').replace('3', '自燃险').replace('4', '玻璃险').replace('5', '座位险').replace('6', '不计免赔险').replace('7', '是否过户').replace('8', '发动机特别损失险'),
            remark: res.data.data.quoteremark,
            baseInsurance: res.data.data.baseinsurance+'元',
            vehicleAndVesselTax: res.data.data.vehicleandvesseltax + '元',
            compulsoryInsurance: res.data.data.compulsoryinsurance + '元',
            insuranceTotal: res.data.data.totalprice + '元',
            replyDetail: res.data.data.replydetail,
            address: res.data.data.address == undefined || res.data.data.address == "" || res.data.data.address == null ? '' : res.data.data.address,
            latitude: res.data.data.latitude,
            longitude: res.data.data.longitude
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.data.msg
          })
        }
        wx.hideToast()
      }
    })
  }
})